package wafa.implementation.gedAPI;

import java.util.Date;
import java.util.Iterator;

import com.ibm.mm.sdk.common.*;
import com.ibm.mm.sdk.server.*;

import wafa.gedInterface.GedService;
import wafa.modelDocument.Content;
import wafa.modelDocument.Document;
import wafa.modelDocument.Index;
import wafa.query.Query;
import wafa.query.Results;

public class ServiceGedImplICM extends GedService {

	String database = "serverged30";
	String userName = "icmadmin";
	String password = "icmadmin";
	String schema = "SCHEMA=" + "ICMADMIN";

	@Override
	public void addDocument(Document document) {
		// TODO Auto-generated method stub
		try {
			DKDatastoreICM dsICM = new DKDatastoreICM();
			dsICM.connect(database, userName, password, schema);
			DKDDO ddo = dsICM.createDDO(document.getFamily().getFamilyName(),
					DKConstant.DK_CM_DOCUMENT);
			
			System.out.println("start"+document.getFamily().getFamilyName());
			
			//add indexes to DDO
			addIndexesToDDO(document,ddo);
			
			DKParts dkParts = (DKParts) ddo.getData(ddo.dataId(
					DKConstant.DK_CM_NAMESPACE_ATTR, DKConstant.DK_CM_DKPARTS));
			// add content to DDO
			addContentToDDO(document, dkParts, dsICM);
			// add attachements to DDO
			addAttachementsToDDO(document, dkParts, dsICM);
			System.out.println("chui laaaaa::::::::::::::::"+dkParts.cardinality());
			//add DDO to CM
			ddo.add();
			
			
	       dsICM.disconnect();
	       dsICM.destroy();
			
			

		} catch (DKException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}



	@Override
	public void deleteDocument(String id) {
		// TODO Auto-generated method stub

	}

	@Override
	public Document getDocumentById(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Results search(Query query) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	
	private void addContentToDDO(Document document, DKParts dkParts,
			DKDatastoreICM dsICM) throws DKException, Exception {

		DKLobICM documentContent = (DKLobICM) dsICM.createDDO("ICMBASE",
				DKConstant.DK_CM_ITEM);
		documentContent.setContent(document.getContent().getContent());
		documentContent.setMimeType(document.getContent().getMimeType());
		dkParts.addElement(documentContent);
		
		
		System.out.println("fin add content");

	}

	private void addAttachementsToDDO(Document document, DKParts dkParts,
			DKDatastoreICM dsICM) throws DKException, Exception {
		DKLobICM attachmentXdo;
		for (Content attachment : document.getAttachments()) {
			attachmentXdo = (DKLobICM) dsICM.createDDO("ICMBASE",
					DKConstant.DK_CM_ITEM);
			attachmentXdo.setContent(attachment.getContent());
			attachmentXdo.setMimeType(attachment.getMimeType());
			dkParts.addElement(attachmentXdo);

		}
		System.out.println("fin add atta");
	}
	
	
	
	
	private void addIndexesToDDO(Document document, DKDDO item) throws DKUsageError{
		for (Index<?> index : document.getFamily().getIndexes()) {
			item.setData(item.dataId(DKConstant.DK_CM_NAMESPACE_ATTR, index.getKey()), index.getValue());
            
		}
		System.out.println("fin add index");
	}

}
